import axios from 'axios'
import QS from 'qs'
import {Message} from 'element-ui'
import router from '../router/index'
import {http9534} from './devlocalport'
const baseURL = http9534

const instance = axios.create({
    // timeout: 20000,
    baseURL,
    headers: {
        'Content-Type': "application/json"
    }
});
const proposal = axios.create({
    // timeout: 20000,
    baseURL,
    headers: {
        'Content-Type': "application/x-www-form-urlencoded"
    }
});
const _upfile = axios.create({
    timeout: 5000,
    baseURL: baseURL,
    responseType: "blob",
    headers: {
        "Content-Type": "application/json"
    }
});
const upfile = axios.create({
    // timeout: 20000,
    baseURL,
    headers: {
        'Content-Type': 'multipart/form-data'
    }
});
instance.interceptors.request.use(
  config => {
   config.headers['Authorization'] = sessionStorage.getItem('token_12')
    return config
  },
  error => {
    return Promise.reject(error)
  }
)
proposal.interceptors.request.use(
  config => {
   config.headers['Authorization'] = sessionStorage.getItem('token_12')
    return config
  },
  error => {
    return Promise.reject(error)
  }
)
_upfile.interceptors.request.use(
  config => {
   config.headers['Authorization'] = sessionStorage.getItem('token_12')
    return config
  },
  error => {
    return Promise.reject(error)
  }
)
upfile.interceptors.request.use(
  config => {
   config.headers['Authorization'] = sessionStorage.getItem('token_12')
    return config
  },
  error => {
    return Promise.reject(error)
  }
)
instance.interceptors.response.use(
        response => {
          const res = response.data;
          if (res.code != 200 && res.code != 0) {
            Message.warning({ message: res.msg });
          } else {
            return response;
          }
        },
        error => {
        //   let code = error.response.data.code;
          let code = error.response;
          console.log(code,'999999999lllllllllllll')
          if (code == 401) {
            Message.warning({ message: "页面token过期，请重新登录！" });
            sessionStorage.removeItem("token");
            this.$router.replace('/login')
          } else {
            Message.error({ message: "服务器异常，请后台处理！" });
          }

          return Promise.reject(error);
        }
      );    
proposal.interceptors.response.use(
        response => {
          const res = response.data;
          if (res.code != 200 && res.code != 0) {
            Message.warning({ message: res.msg });
          } else {
            return response;
          }
        },
        error => {
        //   let code = error.response.data.code;
          let code = error.response;
          console.log(code,'999999999lllllllllllll')
          if (code == 401) {
            Message.warning({ message: "页面token过期，请重新登录！" });
            sessionStorage.removeItem("token");
           this.$router.replace('/login')
          } else {
            Message.error({ message: "服务器异常，请后台处理！" });
          }

          return Promise.reject(error);
        }
      );  

export function get(url, params) {
    return new Promise((resolve, reject) => {
        instance.get(url, {
            params: params
        }).then(res => {
            resolve(res.data);
        }).catch(err => {
            reject(err.data)
        })
    });
}
export function post(url, params) {
    return new Promise((resolve, reject) => {
        proposal.post(url, QS.stringify(params))
            .then(res => {
                resolve(res.data);
            })
            .catch(err => {
                reject(err.data)
            })
    });
}
export function post_json(url, params) {
    return new Promise((resolve, reject) => {
        instance.post(url, params)
            .then(res => {
                resolve(res.data);
            })
            .catch(err => {
                reject(err.data)
            })
    });
}
export function post_excel(url, params) {
    return new Promise((resolve, reject) => {
        _upfile.post(url, params)
            .then(res => {
                resolve(res.data);
            })
            .catch(err => {
                reject(err.data)
            })
    });
}
export function post_form(url, params) {
    return new Promise((resolve, reject) => {
        upfile.post(url, params)
            .then(res => {
                resolve(res.data);
            })
            .catch(err => {
                reject(err.data)
            })
    });
}